package com.quanxi.qxaimain.common.datasource.datasourceAOP.aspect;

import com.quanxi.qxaimain.common.datasource.datasourceAOP.annotation.DataSource;
import com.quanxi.qxaimain.common.datasource.datasourceAOP.multiple.DataSourceContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @Author lyx
 * @create 2022/8/31 10:57
 * 定义切面
 */

@Component
@Slf4j
@Aspect
@Order(-1)
public class DataSourceAspect {
    @Pointcut("@within(com.quanxi.qxaimain.common.datasource.datasourceAOP.annotation.DataSource)||@annotation(com.quanxi.qxaimain.common.datasource.datasourceAOP.annotation.DataSource)")
    public  void pointCut(){}

    @Before("pointCut() && @annotation(dataSource)")
    public  void  doBefore (DataSource dataSource){
        log.info("选择数据源---："+dataSource.value().getDBvalue());
        DataSourceContextHolder.setDataSource(dataSource.value().getDBvalue());
    }

    @After("pointCut()")
    public void doAfter(){
        DataSourceContextHolder.clear();
    }



}